const Koa = require("koa");
const app = new Koa();
const router = require("koa-router")();
const serve = require("koa-static");
const mount = require("koa-mount");
const http = require("http");
const fs = require("fs");
const path = require("path");
const proxy = require("koa-server-http-proxy");
const proxyTable = require("./lib/proxy.js");
app.use(mount("/public", serve(__dirname + "/public")));

Object.keys(proxyTable).forEach((context) => {
    var options = proxyTable[context]
    app.use(proxy(context, options))
})

router.get("/", async (ctx, next) => {
    ctx.type = "html";
    ctx.body = fs.readFileSync(path.join(__dirname, "/index.html"), { encoding: "utf8"});
});

router.get("/favicon.ico", async (ctx, next) => {
    ctx.body = fs.readFileSync(path.join(__dirname, "/favicon.ico"));
});

app.use(router.routes())
   .use(router.allowedMethods());

app.use(async (ctx, next)  =>{
    ctx.type = "html";
    ctx.body = fs.readFileSync(path.join(__dirname, "/index.html"), { encoding: "utf8"});
});

var server = http.createServer(app.callback());
server.listen(2997);
console.log("localhost:2997")